package org.rrs.duker.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.rrs.duker.R;
import org.rrs.duker.pojo.Subscribe;
import org.rrs.duker.ui.activity.HomeActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by foree on 16-7-5.
 * SlideMenu的fragment实现，用来创建侧边list，以及点击切换对应Fragment
 */
public class SlideMenuFragment extends Fragment{
    String[] colors;
    LinearLayout slideMenuLayout;
    ExpandableListView subInfoList;
    HomeActivity mActivity;
    List<String> group_list;
    List<List<Subscribe>> item_list;

    private static final String TAG = SlideMenuFragment.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        slideMenuLayout = (LinearLayout) inflater.inflate(R.layout.slidemenu_list, null);

        subInfoList = (ExpandableListView)slideMenuLayout.findViewById(R.id.expandlistview_slidemenu);

        return slideMenuLayout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //get Activity
        mActivity = (HomeActivity)this.getActivity();

        //init group_list
        group_list = new ArrayList<>();
        group_list.add("default");

        //init item_list
        item_list = new ArrayList<List<Subscribe>>();
        item_list.add(mActivity.getSubscribeList());

        colors = getResources().getStringArray(R.array.color_names);

        ExpandableListAdapter mAdapter = new SumInfoListExpandAdapter();
        subInfoList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                TextView tv = (TextView)view.findViewById(R.id.tv_item_name);
                switchFragment(SumInfoListFragment.newInstance(tv.getText().toString()));
                return false;
            }
        });
        subInfoList.setAdapter(mAdapter);
        subInfoList.expandGroup(0);


    }

    private void switchFragment(Fragment fragment){
        if (getActivity() == null)
            return;

        if (getActivity() instanceof HomeActivity) {
            HomeActivity homeActivity = (HomeActivity) getActivity();
            homeActivity.switchContent(fragment);
        }
    }

    class SumInfoListExpandAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return group_list.size();
        }

        @Override
        public int getChildrenCount(int i) {
            return item_list.get(i).size();
        }

        /**
         *
         * 获取指定组中的数据
         *
         * @param groupPosition
         * @return
         * @see android.widget.ExpandableListAdapter#getGroup(int)
         */
        @Override
        public Object getGroup(int groupPosition) {
            return group_list.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return item_list.get(groupPosition).get(childPosition);
        }

        /**
         * 获取指定组的ID，唯一
         * @param i，组的position
         * @return 组ID
         */
        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            GroupHolder groupHolder = null;

            if (view == null) {
                view = mActivity.getLayoutInflater().from(mActivity).inflate(R.layout.explist_group,null);
                groupHolder = new GroupHolder();
                groupHolder.textView = (TextView)view.findViewById(R.id.tv_group_name);
                view.setTag(groupHolder);
            } else {
                groupHolder = (GroupHolder) view.getTag();
            }

            groupHolder.textView.setText(group_list.get(i));
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            ItemHolder itemHolder = null;

            if (view == null) {
                view = mActivity.getLayoutInflater().from(mActivity).inflate(R.layout.explist_item,null);
                itemHolder = new ItemHolder();
                itemHolder.textView = (TextView)view.findViewById(R.id.tv_item_name);
                view.setTag(itemHolder);
            } else {
                itemHolder = (ItemHolder) view.getTag();
            }

            itemHolder.textView.setText(item_list.get(i).get(i1).getName());
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }

        class GroupHolder{
            public TextView textView;
        }
        class ItemHolder{
            public TextView textView;
        }
    }
}
